package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussServiceClassic;
import cn.pegasus.teamwork.pegasus.entity.BussServiceProject;
import cn.pegasus.teamwork.pegasus.entity.BussUcenterArea;
import cn.pegasus.teamwork.pegasus.mapper.BussServiceProjectMapper;
import cn.pegasus.teamwork.pegasus.service.BussServiceClassicService;
import cn.pegasus.teamwork.pegasus.service.BussServiceProjectService;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.util.DateCompareUtil;
import cn.pegasus.teamwork.util.ErrorCode;
import cn.pegasus.teamwork.util.OSSClientUtil;

@Controller
@RequestMapping("/project")
public class ProjectController extends BaseManagerController<BussServiceProject, BussServiceProjectMapper>{

	private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);
	
	@Autowired
	private BussServiceProjectService projectService;
	
	@Autowired
	private BussServiceClassicService classicServic;
	@Autowired
	private BussUcenterAreaService ucenterAreaService;
	
	/**
	 * 查询服务列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("classic/project-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussServiceProject> dicList = projectService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询服务列表信息异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 添加新增页面
	 */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Long id)throws Exception{
		 List<BussServiceClassic> list = classicServic.findAllList(null);
		 if(null == type){
			 ModelAndView view = new ModelAndView("classic/project-add");
			 view.addObject("list", list);
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("classic/project-edit");
			 BussServiceProject project = projectService.findProjectInfo(id);
			 view.addObject("project", project);
			 view.addObject("list", list);
			 return view;
		 } 
	 }
	 
	 /**
	  * 新增
	  * @RequestParam(value = "file", required = false) MultipartFile file,
	  */
	 @ResponseBody
	 @RequestMapping(value = "/addProject", method = RequestMethod.POST)
	 public String addProject(HttpServletRequest request,
			 @RequestParam(value = "classicId", required = false) String classicId,
			 @RequestParam(value = "areaId", required = false) Integer areaId,
			 @RequestParam(value = "projectName", required = false) String projectName,
			 @RequestParam(value = "projectTimes", required = false) String projectTimes,
			 @RequestParam(value = "serviceStart", required = false) String serviceStart,
			 @RequestParam(value = "serviceEnd", required = false) String serviceEnd,
			 @RequestParam(value = "projectPrice", required = false) Double projectPrice,
			 @RequestParam(value = "projectOriginalPrice", required = false) Double projectOriginalPrice,
			 @RequestParam(value = "projectDesc", required = false) String projectDesc){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussServiceProject project = new BussServiceProject();
        	project.setClassicId(classicId);
        	project.setAreaId(areaId);
        	project.setProjectName(projectName);
        	project.setProjectTimes(projectTimes);
        	if(StringUtils.isNotBlank(serviceStart)){
        		project.setServiceStart(DateCompareUtil.getDateToString(serviceStart));
        	}
        	if(StringUtils.isNotBlank(serviceEnd)){
        		project.setServiceEnd(DateCompareUtil.getDateToString(serviceEnd));
        	}
        	project.setProjectPrice((long)(projectPrice*100));
        	project.setProjectDesc(projectDesc);
        	project.setProjectOriginalPrice((long)(projectOriginalPrice*100));
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String projectImages = util.uploadImg2Oss(file);
            	project.setProjectImages(projectImages);
        	}
        	projectService.addProject(getAdmin().getId(), project);
        } catch (Exception e) {
            logger.error("新增服务项目异常", e);
            return "1001";
        }
        String dd= ErrorCode.getCallBackStr(200, null);
        return "200";
	 }
	 
	 /**
	  * 修改
	  */
	 @ResponseBody
	 @RequestMapping(value="/updateProject", method = RequestMethod.POST)
	 public String updateProject(HttpServletRequest request,
			 @RequestParam(value = "classicId", required = false) String classicId,
			 @RequestParam(value = "areaId", required = false) Integer areaId,
			 @RequestParam(value = "projectName", required = false) String projectName,
			 @RequestParam(value = "projectTimes", required = false) String projectTimes,
			 @RequestParam(value = "serviceStart", required = false) String serviceStart,
			 @RequestParam(value = "serviceEnd", required = false) String serviceEnd,
			 @RequestParam(value = "projectPrice", required = false) Double projectPrice,
			 @RequestParam(value = "projectDesc", required = false) String projectDesc,
			 @RequestParam(value = "projectOriginalPrice", required = false) Double projectOriginalPrice,
			 @RequestParam(value = "id", required = false) Long id){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussServiceProject project = new BussServiceProject();
        	project.setId(id);
        	project.setClassicId(classicId);
        	project.setAreaId(areaId);
        	project.setProjectName(projectName);
        	project.setProjectTimes(projectTimes);
        	if(StringUtils.isNotBlank(serviceStart)){
        		project.setServiceStart(DateCompareUtil.getDateToString(serviceStart));
        	}
        	if(StringUtils.isNotBlank(serviceEnd)){
        		project.setServiceEnd(DateCompareUtil.getDateToString(serviceEnd));
        	}
        	project.setProjectPrice((long)(projectPrice*100));
        	project.setProjectOriginalPrice((long)(projectOriginalPrice*100));
        	project.setProjectDesc(projectDesc);
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String projectImages = util.uploadImg2Oss(file);
            	project.setProjectImages(projectImages);
        	}
        	projectService.updateProject(getAdmin().getId(), project);
        } catch (Exception e) {
            logger.error("修改服务项目异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 删除
	  */
	 @ResponseBody
	 @RequestMapping(value="/delete")
	 public Map<String,Object> delete(@RequestParam(value="id",required = false) Long id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	projectService.delete(getAdmin().getId(),id);
        	resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("删除区域异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
}
